package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.bridge.util.DateUtils;
import com.tamartech.greendb.loader.ElectoralRollLoader;

public class ElectoralRollImporter
{
    private static Logger logger = Logger.getLogger (ElectoralRollImporter.class);
    private static SimpleDateFormat formatRollDate = new SimpleDateFormat ("dd/MM/yyyy");
    private static SimpleDateFormat formatDOB = new SimpleDateFormat ("dd/MM/yyyy");
    
    private static String HEAD_POLLING_DISTRICT = "Elector Number Prefix";
    private static String HEAD_VOTER_NUMBER = "Elector Number";
    private static String HEAD_VOTER_NUMBER_SUFFIX = "Elector Number Suffix";
    private static String HEAD_ELECTOR_MARKERS = "Elector Markers";
    private static String HEAD_DOB = "Elector DOB";
    private static String HEAD_SURNAME = "Elector Surname";
    private static String HEAD_FORENAMES = "Elector Forename";
    private static String HEAD_POSTCODE = "PostCode";
    private static String HEAD_ADDRESS1 = "Address1";
    private static String HEAD_ADDRESS2 = "Address2";
    private static String HEAD_ADDRESS3 = "Address3";
    private static String HEAD_ADDRESS4 = "Address4";
    private static String HEAD_ADDRESS5 = "Address5";
    private static String HEAD_ADDRESS6 = "Address6";  
    
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatRollDate.parse(args[2]);
        File logDir = new File (args[3]);
        
        PrintWriter newAddressLog = new PrintWriter (new File (logDir, "NewAddresses-" + wardName + ".log"));
        PrintWriter newPersonLog = new PrintWriter (new File (logDir, "NewPersons-" + wardName + ".log"));
	    
        ElectoralRollLoader loader = new ElectoralRollLoader (newPersonLog, newAddressLog);
        loader.begin(wardName, rollDate);
        
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
        
        
        while(csv.getLine() != null)
        {
            String pollingDistrict = csv.getValueByLabel(HEAD_POLLING_DISTRICT).trim();
            int voterNumber = Integer.valueOf(csv.getValueByLabel(HEAD_VOTER_NUMBER).trim());
            
            Integer voterNumberSuffix = Integer.valueOf(csv.getValueByLabel(HEAD_VOTER_NUMBER_SUFFIX).trim());;
            
            if (voterNumberSuffix == 0)
                voterNumberSuffix = null;
            
            loader.beginEntry(pollingDistrict, voterNumber, voterNumberSuffix);
            
            loader.putName (csv.getValueByLabel(HEAD_FORENAMES).trim(), 
                            csv.getValueByLabel(HEAD_SURNAME).trim());
            
            loader.putAddress(csv.getValueByLabel(HEAD_ADDRESS1).trim(), 
                              csv.getValueByLabel(HEAD_ADDRESS2).trim(), 
                              csv.getValueByLabel(HEAD_ADDRESS3).trim(), 
                              csv.getValueByLabel(HEAD_ADDRESS4).trim(), 
                              csv.getValueByLabel(HEAD_ADDRESS5).trim(), 
                              csv.getValueByLabel(HEAD_ADDRESS6).trim(),
                              csv.getValueByLabel(HEAD_POSTCODE).trim());
            
            Date dob = null;
            
            String strDOB = csv.getValueByLabel(HEAD_DOB).trim();
            
            if (strDOB.length() > 0)
                dob = formatDOB.parse(strDOB);
            
            if (dob != null)
                loader.put18thBirthday (DateUtils.addYears(dob, 18));
            
            // should do something more sophisticated than this, but for the moment just pass in the values
            String voteType = csv.getValueByLabel(HEAD_ELECTOR_MARKERS).trim();
            
            if (voteType.length() > 0)
                loader.putVoteType(voteType);
            
            loader.endEntry();
        }
        
        csv.close();

        loader.end();
        
        newAddressLog.close();
        newPersonLog.close();
	}	
} 